package com.huluobo.lc.kotlinim1812a.ui.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.lc.kotlinim1812a.R
import com.huluobo.lc.kotlinim1812a.adapter.ContactListAdapter
import com.huluobo.lc.kotlinim1812a.adapter.EMContactListenerAdapter
import com.huluobo.lc.kotlinim1812a.base.BaseFragment
import com.huluobo.lc.kotlinim1812a.contract.contract.ContactContact
import com.huluobo.lc.kotlinim1812a.contract.contract.ContactPresenter
import com.huluobo.lc.kotlinim1812a.ui.activity.AddFriendActivity
import com.huluobo.lc.kotlinim1812a.wiget.SlideBar
import com.hyphenate.EMContactListener
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * @author Lc
 * @description: 联系人界面
 * @date :2021/6/23 9:59
 */
class ContactFragment : BaseFragment(), ContactContact.View {
    override fun getLayoutResId(): Int = R.layout.fragment_contacts

    val presenter = ContactPresenter(this)

    override fun init() {
        super.init()
        headerTitle.text = getString(R.string.contact)
        add.visibility = View.VISIBLE

        swipeRefreshLayout.apply {
            setColorSchemeColors(resources.getColor(R.color.qq_blue))
            isRefreshing = true
            setOnRefreshListener { presenter.loadContacts() }
        }

        recyclerView.apply {
            //当Recyclerview长度发生变化时,内部会做优化处理
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context)
            adapter = ContactListAdapter(context, presenter.contactListItems)
        }

        presenter.loadContacts()

        EMClient.getInstance().contactManager()
            .setContactListener(object : EMContactListenerAdapter() {
                override fun onContactDeleted(username: String?) {
                    //当感知到用户被删除时,重新获取联系人数据
                    presenter.loadContacts()
                }
            })

        slideBar.onSectionChangeListener = object : SlideBar.OnSectionChangeListener {
            override fun onSectionChange(firstLetter: String) {
                section.visibility = View.VISIBLE
                section.text = firstLetter
                //平滑滑动
                recyclerView.smoothScrollToPosition(getPosition(firstLetter))
            }

            override fun onSlideFinish() {
                section.visibility = View.GONE
            }

        }

        add.setOnClickListener {
            context?.startActivity<AddFriendActivity>()
        }

    }

    private fun getPosition(firstLetter: String): Int =
        presenter.contactListItems.binarySearch { it ->
            it.firstLetter.minus(firstLetter[0])
        }

    override fun onLoadContactsSuccess() {
        //加载成功关闭下拉刷新
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.isRefreshing = false
            //刷新适配器
            recyclerView.adapter?.notifyDataSetChanged()
        }
    }

    override fun onLoadContactsFailed() {
        //关闭下拉刷新
        swipeRefreshLayout.isRefreshing = false
        //toast
        context?.toast(R.string.load_contacts_failed)
    }
}